package com.netease.nim.demo.team.adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.widget.FrameLayout;

import com.netease.nim.demo.common.adapter.TAdapter;
import com.netease.nim.demo.common.adapter.TAdapterDelegate;
import com.netease.nim.demo.team.ui.TeamInfoGridView;
import com.netease.nim.demo.team.viewholder.TeamMemberHolder;
import com.netease.nim.demo.team.viewholder.TeamMemberHolder.ItemClickListener;
import com.shwread.android.qysw10000204.R;

import java.util.List;

import logic.util.ScreenUtil;


public class TeamMemberAdapter
        extends
        TAdapter
{
	
	/**
	 * 当前GridView显示模式：显示讨论组成员，正在移除讨论组成员
	 */
	public static enum Mode {
		NORMAL, DELETE
	}
	
	/**
	 * 每个Item的类型：讨论组成员，添加成员，移除成员
	 */
	public static enum TeamMemberItemTag {
		NORMAL, ADD, DELETE
	}
	
	/**
	 * GridView数据项
	 */
	public static class TeamMemberItem
	{
		private TeamMemberItemTag tag;
		private String            tid;
		private String            account;
		private String            desc;
		private String            headImage;
		private String            nickName;
		private String            staffName;
		private int               teamRole;
		
		public String getHeadImage() {
			return this.headImage;
		}
		
		public void setHeadImage(String headImage) {
			this.headImage = headImage;
		}
		
		public String getNickName() {
			return this.nickName;
		}
		
		public void setNickName(String nickName) {
			this.nickName = nickName;
		}
		
		public String getStaffName() {
			return staffName;
		}
		
		public void setStaffName(String staffName) {
			this.staffName = staffName;
		}
		
		public TeamMemberItem(TeamMemberItemTag tag, String tid, String account, String desc, String headImage, String nickName, String staffName, int teamRole) {
			this.tag = tag;
			this.tid = tid;
			this.account = account;
			this.desc = desc;
			this.headImage = headImage;
			this.nickName = nickName;
			this.staffName = staffName;
			this.teamRole = teamRole;
		}
		
		public TeamMemberItemTag getTag() {
			return tag;
		}
		
		public String getTid() {
			return tid;
		}
		
		public String getDesc() {
			return desc;
		}
		
		public String getAccount() {
			return account;
		}
		
		public int getTeamRole() {
			return teamRole;
		}
	}
	
	/**
	 * 群成员移除回调函数
	 */
	public static interface RemoveMemberCallback
	{
		public void onRemoveMember(String account);
	}
	
	public static interface AddMemberCallback
	{
		public void onAddMember();
	}
	
	private Context              context;
	
	private Mode                 mode = Mode.NORMAL;
	
	private RemoveMemberCallback removeMemberCallback;
	
	private AddMemberCallback    addMemberCallback;
	
	public Mode getMode() {
		return mode;
	}
	
	public void setMode(Mode mode) {
		this.mode = mode;
	}
	
	public boolean switchMode() {
		if (getMode() == Mode.DELETE)
		{
			setMode(Mode.NORMAL);
			notifyDataSetChanged();
			return true;
		}
		return false;
	}
	
	public RemoveMemberCallback getRemoveMemberCallback() {
		return removeMemberCallback;
	}
	
	public AddMemberCallback getAddMemberCallback() {
		return addMemberCallback;
	}
	
	public TeamMemberAdapter(Context context, List<?> items, TAdapterDelegate delegate, RemoveMemberCallback removeMemberCallback, AddMemberCallback addMemberCallback) {
		super(context, items, delegate);
		this.context = context;
		this.removeMemberCallback = removeMemberCallback;
		this.addMemberCallback = addMemberCallback;
	}
	
	private TeamMemberHolder.ItemClickListener itemClickListener;
	
	public void setItemClickListener(ItemClickListener listener) {
		itemClickListener = listener;
	}
	
	private TeamMemberHolder.TeamMemberHolderEventListener teamMemberHolderEventListener;
	
	public void setEventListener(TeamMemberHolder.TeamMemberHolderEventListener eventListener) {
		this.teamMemberHolderEventListener = eventListener;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final View view = super.getView(position, convertView, parent);
		if (teamMemberHolderEventListener != null)
		{
			((TeamMemberHolder) view.getTag()).setEventListener(teamMemberHolderEventListener);
		}
		if (null != itemClickListener)
		{
			((TeamMemberHolder) view.getTag()).setItemClickListener(itemClickListener);
		}
		if (position > 7)
		{
			view.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener()
			{
				
				@TargetApi (Build.VERSION_CODES.JELLY_BEAN)
				@Override
				public boolean onPreDraw() {
					if (!isOK)
					{
						itemHeight = view.getHeight();
						LayoutParams lp = gridFly.getLayoutParams();
						if (android.os.Build.VERSION.SDK_INT > 15)
						{
							gridInitHeight = view.getHeight() * 2 + gridFly.getPaddingBottom() + gridFly.getPaddingTop() + gridV.getVerticalSpacing();
						}
						else
						{
							gridInitHeight = view.getHeight() * 2 + gridFly.getPaddingBottom() + gridFly.getPaddingTop() + ScreenUtil.dip2px(context.getResources().getDimension(R.dimen.team_info_grid_verticalspacing));//TODO
						}
						lp.height = gridInitHeight;
						gridFly.setLayoutParams(lp);
						isOK = true;
					}
					return true;
				}
			});
		}
		
		return view;
	}
	
	public int               itemHeight     = -1;
	public int               gridInitHeight = -1;
	private FrameLayout      gridFly;
	private TeamInfoGridView gridV;
	private boolean          isOK           = false;
	
	public void setOutView(FrameLayout gridFly) {
		this.gridFly = gridFly;
	}
	
	public void setOutViewG(TeamInfoGridView gridV) {
		this.gridV = gridV;
	}
	
}
